import 'package:bxultimate/app/theme/app_theme.dart';
import 'package:bxultimate/app/theme/gaps.dart';
import 'package:bxultimate/domain/home/search_hint.dart';
import 'package:bxultimate/resources/assets.dart';
import 'package:bxultimate/widgets/mk_page.dart';
import 'package:bxultimate/widgets/mk_search_bar.dart';
import 'package:bxultimate/widgets/text/text_highlight.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'controller.dart';

class HomeSearchPage extends StatefulWidget {
  const HomeSearchPage({Key? key}) : super(key: key);

  @override
  State<HomeSearchPage> createState() => _HomeSearchPageState();
}

class _HomeSearchPageState extends State<HomeSearchPage> {
  final controller = Get.find<HomeSearchController>();
  final state = Get.find<HomeSearchController>().state;

  @override
  Widget build(BuildContext context) {
    return MkPage(
        child: Scaffold(
      backgroundColor: Colors.transparent,
      appBar: MkSearchBar(
        autoFocus:true,
        hintText: '今天搜点什么呢',
        editingController: controller.editingController,
        onPressed: controller.onSearch,
      ),
      body: SingleChildScrollView(
        child: Obx(()=>_buildBody(context)),
      ),
    ));
  }

  Widget _buildBody(BuildContext context) {
    return state.searchText == '' ?Container(
      padding: EdgeInsets.symmetric(horizontal: AppTheme.iDefaultPadding),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Gaps.vGap12,
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                '历史记录',
                style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
              ),
              Image.asset(Assets.homeWorkDt, width: 18)
            ],
          ),
          Gaps.vGap12,
          Wrap(
            direction: Axis.horizontal,
            alignment: WrapAlignment.start,
            spacing: 10.0,
            runSpacing: 10.0,
            children: [
              ...state.historySearchList.map((e) => GestureDetector(onTap:(){controller.onSearch(e);},child: TDTag(e, isLight: true, shape: TDTagShape.round,))).toList(),
            ],
          )
        ],
      ),
    ) : _buildSearchResult();
  }

  _buildSearchResult(){
    return Column(
      children: [
        ...state.searchHintList.map((e) => _buildSearchHintList(e)
        ).toList()
      ],
    );
  }

  _buildSearchHintList(SearchHint searchHint){
    return GestureDetector(
      onTap: () {
        controller.toDetail(searchHint);
      },
      child: Container(
        width: double.infinity,
        padding: EdgeInsets.symmetric(horizontal: AppTheme.iDefaultPadding,vertical: 12),
        // margin: EdgeInsets.symmetric(horizontal: AppTheme.iDefaultPadding),
        decoration: BoxDecoration(
          color: Colors.white,
          border: Border(
            bottom: BorderSide(color: Colors.grey, width: 0.5)
          )
        ),
        child: TextHighlight(searchHint.title,state.searchText,TextStyle(color: Colors.black),TextStyle(color: Colors.redAccent)),
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<HomeSearchController>();
    super.dispose();
  }
}
